define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campus_course_mall_goods/index' + location.search,
                    add_url: 'campus_course_mall_goods/add',
                    edit_url: 'campus_course_mall_goods/edit',
                    del_url: 'campus_course_mall_goods/del',
                    //multi_url: 'campus_course_mall_goods/multi',
                    //import_url: 'campus_course_mall_goods/import',
                    table: 'campus_course_mall_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'goods_name', title: __('C_id')},
                        {field: 'type', title: __('类型'), searchList: {"1":__('课程'),"2":__('课程套餐')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('隐藏'),"1":__('展示')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $('#mallgoodschoose').click(function(){
                var url = '/manystore/campus_course_choose/index';
                if(!url) return false;
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ['900px','600px'],
                    callback:function(value){
                        //这个value就是传上来的值
                        var index = value .lastIndexOf("+")
                        var type =value .substring(0,index);
                        var value =value .substring(index+1,value.length);
                        var arr = $.parseJSON(value);
                        console.log(arr)
                        if(arr.length > 0){
                            var id = arr[0]['id'];
                            mall_goods_choose(id,type)
                        }

                        //Layer.alert("接收到回传数据：" + value, {title: "回传数据"});
                    }
                };
                Fast.api.open(url,'选择项目',options);
            })

            function mall_goods_choose(id,type){
                jQuery.post('campus_course_mall_goods/mall_goods_choose', {
                    id:id,
                    type:type,
                }, function(data) {
                    console.log(data.type)
                    console.log(data)
                    if(data.code == 1){
                        if(type == 1){//课程，没有单独的售价
                            if(data.data.course_series_id != null){
                                $("#series").show();
                            }
                        }else if(type == 2){
                            $("#series").hide();
                            $("#c-image").val('');
                            $("#c-content").val('');
                            $("#c-price").val(data.data.pack_price);
                        }
                        $("#goods_name_ajax").html(data.data.type_txt+'-'+data.data.name);
                        $("#c_id").val(data.data.id);
                        $("#type").val(type);
                        $("#course_series_id").val(data.data.course_series_id);
                    }else{
                        alert(data.msg);
                    }
                });
            }
            $('#series').click(function(){
                var course_series_id = $("#course_series_id").val()
                jQuery.post('campus_course_mall_goods/course_series', {
                    id:course_series_id,
                }, function(data) {
                    console.log(data.data.content)
                    $("#c-image").val(data.data.cover_picture);
                    $("#c-content").val(data.data.content);
                });
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "input[name='row[period_status]']", function(){
                    console.log($(this).val())
                    var period_status = $(this).val();
                    if(period_status == '1'){
                        $("#period_show").show();
                    }else if(period_status == '0'){
                        $("#period_show").hide();
                    }
                });

                /*添加按课时收费*/
                var h_rows_i = $("#c_hour_num").val();
                $('#new_hour').on('click',function(){
                    h_rows_i++;
                    $("#c_hour_num").val(h_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[h_name_'+h_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[h_num_'+h_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[h_total_price_'+h_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[h_price_'+h_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-1">' +
                        '<span class="btn btn-danger span_del_btn del_h" id="h_del_'+h_rows_i+'">删除</span>' +
                        '</div>' +
                        '</div>';
                    $('#hour .tab_div ').append(html);
                });
                $(document).on('click', '.del_h', function(){
                    $(this).parents('.row_div').remove();
                })

                $(document).on("change", "input[name='row[monthly_status]']", function(){
                    console.log($(this).val())
                    var monthly_status = $(this).val();
                    if(monthly_status == '1'){
                        $("#monthly_show").show();
                    }else if(monthly_status == '0'){
                        $("#monthly_show").hide();
                    }
                });
                /*添加按月收费*/
                var m_rows_i = $("#c_monthly_num").val();
                $('#new_monthly').on('click',function(){
                    m_rows_i++;
                    $("#c_monthly_num").val(m_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_name_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_num_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_total_price_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_price_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-1">' +
                        '<span class="btn btn-danger span_del_btn del_m" id="m_del_'+m_rows_i+'">删除</span>' +
                        '</div>' +
                        '</div>';
                    $('#monthly .tab_div ').append(html);
                });
                $(document).on('click', '.del_m', function(){
                    $(this).parents('.row_div').remove();
                })

                $(document).on("change", "input[name='row[per_diem_status]']", function(){
                    console.log($(this).val())
                    var per_diem_status = $(this).val();
                    if(per_diem_status == '1'){
                        $("#per_diem_show").show();
                    }else if(per_diem_status == '0'){
                        $("#per_diem_show").hide();
                    }
                });
                /*添加按天收费*/
                var pe_rows_i = $("#c_per_diem_num").val();
                $('#new_per_diem').on('click',function(){
                    pe_rows_i++;
                    $("#c_per_diem_num").val(pe_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_name_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_num_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_total_price_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_price_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-1">' +
                        '<span class="btn btn-danger span_del_btn del_pe" id="pe_del_'+pe_rows_i+'">删除</span>' +
                        '</div>' +
                        '</div>';
                    $('#per_diem .tab_div ').append(html);
                });
                $(document).on('click', '.del_pe', function(){
                    $(this).parents('.row_div').remove();
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});